package com.example.qimo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.qimo.Db.Task;
import com.example.qimo.bean.Code;

public class FindPassword extends AppCompatActivity {
    private EditText find_phone,find_code,find_pswd;
    private Button find_ok,find_cancle,find_bt_code;
    private String resultcode,url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);

        find_phone=findViewById(R.id.find_phone);
        find_pswd=findViewById(R.id.find_password);
        find_code=findViewById(R.id.find_code);
        find_ok=findViewById(R.id.find_ok);
        find_cancle=findViewById(R.id.find_cancle);
        find_bt_code=findViewById(R.id.find_bt_code);
        find_bt_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=find_phone.getText().toString();
                if (!phone.equals("")){
                    url="https://www.wiod.cn/phonecheck?phone="+phone;
                    new Task(new Task.CallBack() {//检查手机号是否已注册
                        @Override
                        public void getResult(Code result) {
                            resultcode=result.getCode();
                            System.out.println(resultcode+"-------------------------------");
                            if (resultcode.equals("200")){
                                find_bt_code.setEnabled(false);
                                url="https://www.wiod.cn/sms/findpswd?phone="+phone;
                                new Task(new Task.CallBack() {
                                    @Override
                                    public void getResult(Code result) {
                                        resultcode=result.getCode();
                                        System.out.println(resultcode+"-------------------------------");
                                    }
                                }).execute(url);
                                Toast.makeText(FindPassword.this, "验证码已发送！", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(FindPassword.this, "手机号未注册！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }).execute(url);
                }else{
                    Toast.makeText(FindPassword.this, "请输入手机号！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        find_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=find_phone.getText().toString();//获取手机号
                String code=find_code.getText().toString();//获取验证码
                String password=find_pswd.getText().toString();
                String url="https://www.wiod.cn/findpswd/?phone="+phone+"&pswd="+password;
                if (phone.equals("") || code.equals("")||password.equals("")){
                    Toast.makeText(FindPassword.this, "所有项不能为空！", Toast.LENGTH_SHORT).show();
                }else if(!code.equals(resultcode)){
                    Toast.makeText(FindPassword.this, "验证码错误！", Toast.LENGTH_SHORT).show();
                }else{
                    new Task(new Task.CallBack() {
                        @Override
                        public void getResult(Code result) {
                            String resultmsg=result.getCode();
                            System.out.println(resultmsg);

                                Toast.makeText(FindPassword.this, "密码修改完成！", Toast.LENGTH_SHORT).show();
                                finish();

                        }
                    }).execute(url);
                }
            }
        });
        find_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}